import {
  Injectable,
  CanActivate,
  ExecutionContext,
  UnauthorizedException,
} from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    const token = request.headers.authorization;

    if (!token) {
      throw new UnauthorizedException('未提供授权令牌');
    }

    // 简单的令牌验证逻辑
    // 在实际应用中，你应该验证JWT令牌或其他认证机制
    if (token === 'Bearer mock-token') {
      return true;
    }

    throw new UnauthorizedException('无效的授权令牌');
  }
}